package cn.wolfcode.crm.web.controller;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping
public class IndexController {

    @RequestMapping("")
    public String main(){
        return "index";
    }

    @RequestMapping("index2")
    public String index2(){
        return "index2";
    }
    @RequestMapping("main")
    public String index(){
        return "index";
    }

    @RequestMapping("login")
    public String login(Model model, HttpServletRequest req) {
        //如果登陆失败从request中获取认证异常信息，shiroLoginFailure就是shiro异常类的全限定名
        String shiroLoginFailure = (String) req.getAttribute("shiroLoginFailure");
        //根据shiro返回的异常类路径判断，抛出指定异常信息
        if (shiroLoginFailure != null) {
            if (UnknownAccountException.class.getName().equals(shiroLoginFailure)
                    || IncorrectCredentialsException.class.getName().equals(shiroLoginFailure)) {
                //最终会抛给异常处理器
                model.addAttribute("msg", "用户名/密码错误");
            } else {
                model.addAttribute("msg", "其他异常信息");//最终在异常处理器生成未知错误
            }
        }
        //此方法不处理登陆成功（认证成功），shiro认证成功会自动跳转到上一个请求路径
        //登陆失败还到login页面
        return "forward:login.jsp";
    }
}
